package com.shanghaionstar.sqlite;

import android.content.Context;

import com.onstar.cn.sqlite.entities.OvdHistory;
import com.onstar.cn.sqlite.xutil.db.sqlite.Selector;
import com.onstar.cn.sqlite.xutil.db.sqlite.WhereBuilder;
import com.onstar.cn.sqlite.xutil.exception.DbException;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: DatabaseOvdHistory
 * @Description: 车况数据存储
 * @author Xiaofeng Feng
 * @date 2015年8月14日 下午2:15:18 
 */
public class DataRefreshDBService extends BaseSqlImp {

	public DataRefreshDBService(Context context) {
		super(context);
	}

	/**
	 *
	* (non-Javadoc)
	* <p>Title: insert</p>
	* <p>Description: </p>
	* @param p_Info
	* @return
	* @see com.onstar.cn.sqlite.template.impl.BaseTemplateImpl#insert(Object)
	 */
	public long insert(OvdHistory p_Info) {
		long rowId = 0;
		try {
			mDbUtils.save(p_Info);
			rowId = mDbUtils.count(OvdHistory.class);
		} catch (Exception e) {
			e.printStackTrace();
		} catch (OutOfMemoryError e) {
			e.printStackTrace();
		}
		return rowId;
	}

	/**
	 *
	* (non-Javadoc)
	* <p>Title: update</p>
	* <p>Description: </p>
	* @param p_Info
	* @return
	* @see com.onstar.cn.sqlite.template.impl.BaseTemplateImpl#update(Object)
	 */
	public void update(OvdHistory p_Info) {
//		return super.update(p_Info, "vin=?", new String[] { p_Info.getVin() });
		try {
			mDbUtils.update(p_Info, WhereBuilder.b("vin","=",p_Info.getVin()));
		} catch (DbException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	* @Title: delete 
	* @Description: 
	* @param @param id    设定文件 
	* @return void    返回类型 
	* @throws
	 */
	public void delete(String id) {
//		super.delete("_id=? ", new String[] { id });
		try {
//			mDbUtils.delete(OvdHistory.class,WhereBuilder.b("_id","=",id));
			mDbUtils.deleteById(OvdHistory.class,id);
		} catch (DbException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	* @Title: checkDataIfExits 
	* @Description:  info
	* @param @param info
	* @param @return    设定文件 
	* @return boolean    返回类型 
	* @throws
	 */
	public boolean checkDataIfExits(OvdHistory info) {
		return checkDataIfExits(info.getVin());
	}

	/**
	 * 
	* @Title: checkDataIfExits 
	* @Description:  info
	* @param @param vin
	* @param @return    设定文件 
	* @return boolean    返回类型 
	* @throws
	 */
	public boolean checkDataIfExits(String vin) {
		boolean result = false;
		List<OvdHistory> list = new ArrayList<OvdHistory>();
		try {
//			list = getAll(null, "vin=? ", new String[] { vin }, null, null, null, null);
			list = mDbUtils.findAll(Selector.from(OvdHistory.class).where("vin","=",vin));
			if(list.size()>0){
				if(!list.get(0).getVin().equals("")){
					result=true;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 
	* @Title: getAllOvdHistory 
	* @Description:  查询所有未读或已读消息isRead='true' or isRead='false'
	* @param @param vin
	* @param @param isRead
	* @param @return    设定文件 
	* @return List<OvdHistory>    返回类型 
	* @throws
	 */
	public List<OvdHistory> getAllOvdHistory(String vin, String isRead) {
		List<OvdHistory> list = new ArrayList<OvdHistory>();
		try {
//			String orderBy = null;
//				orderBy = OvdHistory.ISREAD + " desc ";
//			list = getAll(null, "vin=? and isRead=? ", new String[] { vin , isRead}, null, null, orderBy, null);
			list = mDbUtils.findAll(Selector.from(OvdHistory.class).where("vin","=",vin).and("isRead","=",isRead).orderBy(OvdHistory.ISREAD,true));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * 
	* @Title: getOvdHistory 
	* @Description: 根据vin号查询单个历史
	* @param @param vin
	* @param @return    设定文件 
	* @return OvdHistory    返回类型 
	* @throws
	 */
	public OvdHistory getOvdHistory(String vin) {
		List<OvdHistory> list = new ArrayList<OvdHistory>();
		try {
//			String orderBy = null;
//				orderBy = OvdHistory.CREATEDATE + " desc ";
//			list = getAll(null, "vin=? ", new String[] { vin==null?"":vin }, null, null, orderBy, null);
			list = mDbUtils.findAll(Selector.from(OvdHistory.class).where("vin","=",vin).orderBy(OvdHistory.CREATEDATE,true));
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(list != null && list.size() > 0) {
			return list.get(0);
		}else {
			return null;
		}
	}

	 /**
	  * 
	 * @Title: getAllOvdHistory 
	 * @Description:  查询所有消息
	 * @param @param vin
	 * @param @return    设定文件 
	 * @return List<OvdHistory>    返回类型 
	 * @throws
	  */
	public List<OvdHistory> getAllOvdHistory(String vin) {
		List<OvdHistory> list = new ArrayList<OvdHistory>();
		try {
//			String orderBy = null;
//				orderBy = OvdHistory.ISREAD + " desc ";
//			list = getAll(null, "vin=? ", new String[] { vin }, null, null, orderBy, null);
			list = mDbUtils.findAll(Selector.from(OvdHistory.class).where("vin","=",vin).orderBy(OvdHistory.ISREAD,true));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
}
